package com.ceba.test.modules.email.dao;

import java.util.ArrayList;
import java.util.List;

import com.ceba.cebautils.DateUtil;
import org.json.JSONObject;

import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.email.entity.MsgEmailSet;
import com.ceba.test.modules.email.entity.MsgMailSend;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.XObject;

public class MsgEmailSetDao extends IBaseDao {
	public  MsgEmailSet getMsgEmailSetById(String id) throws Exception{
		MsgEmailSet emailSet = new MsgEmailSet();
		emailSet.setId(id);
		DataTable dt = getDataTableById(id);
		if(dt !=null && dt.getRowCount()>0){
			DataRow dr = dt.getRow(0);
			copyDrToMsgEmailSet(dr, emailSet);
		}
		return emailSet;
	}

    private void copyDrToMsgEmailSet(DataRow dr,MsgEmailSet mes){
    	JSONObject dd = dr.toJSONObject();
    	mes.setId(dd.has("ID")?dr.getString("ID"):"");
		mes.setCallName(dd.has("CALL_NAME")?dr.getString("CALL_NAME"):"");
		mes.setContextTemplate(dd.has("CONTEXT_TEMPLATE")?dr.getString("CONTEXT_TEMPLATE"):"");
		mes.setEmailName(dd.has("EMAIL_NAME")?dr.getString("EMAIL_NAME"):"");
		mes.setEmailType(dd.has("EMAIL_TYPE")?dr.getString("EMAIL_TYPE"):"");
		mes.setTitle(dd.has("TITLE")?dr.getString("TITLE"):"");
		mes.setCreateTime(dd.has("CREATE_TIME")  ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(), DateUtil.YYYY_MM_DD_HH_MM) : "");
		mes.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		mes.setUpdateTime(dd.has("UPDATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("UPDATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		mes.setUpdateUser(dd.has("UPDATE_USER")?dr.getString("UPDATE_USER"):"");
    }
    
    private void copyDrToMsgEmailSend(DataRow dr,MsgMailSend mms){
    	JSONObject dd = dr.toJSONObject();
    	mms.setId(dd.has("ID")?dr.getString("ID"):"");
    	mms.setName(dd.has("NAME")?dr.getString("NAME"):"");
		mms.setEmail(dd.has("EMAIL")?dr.getString("EMAIL"):"");
		mms.setCreateTime(dd.has("CREATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		mms.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
    }
    
    
	public List<MsgMailSend> getSendEMail() {
    	List<MsgMailSend> mmsList = new ArrayList();
		DataTable dt = SqlSessionManager.getDataTable("SELECT distinct EMAIL FROM msg_mail_send");
		if (dt != null && dt.getRowCount() > 0) {
			for( int i = 0 ; i < dt.getRowCount() ; i++){
				DataRow dr = dt.getRow(i);
				MsgMailSend mes = new MsgMailSend();
				copyDrToMsgEmailSend(dr,mes);
				mmsList.add(mes);
			}
		}
		return mmsList;
	}
    
    
	public  DataTable getDataTableById(String Id) throws Exception{
       Sql sql  = new Sql("SELECT * FROM msg_mail_set where ID='"+Id+"'");
		return SqlSessionManager.getDataTable(sql);
	}
    
	
	/**
	 * 获取所有
	 * @return
	 * @throws Exception
	 */
	public  DataTable getDataTableType(String type) throws Exception{
		Sql sql  = new Sql("SELECT *  FROM msg_mail_set where EMAIL_TYPE='"+type+"'");
		return SqlSessionManager.getDataTable(sql);

	}
	
	/**
	 * 修改
	 * @return
	 */
	public boolean modify(MsgEmailSet msgEmail){
		IStore store = StoreManager.create("msg_mail_set");
		XObject object = new XObject("ID",msgEmail.getId());
		object.put("TITLE", msgEmail.getTitle());
		object.put("EMAIL_NAME", msgEmail.getEmailName());
		object.put("CONTEXT_TEMPLATE", msgEmail.getContextTemplate());
		object.setUpdateUser();
		object.setUpdateTime();
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取所有
	 * @return
	 * @throws Exception
	 */
	public  DataTable getDataTable(String type) throws Exception{
		Sql sql  = new Sql("SELECT *  FROM msg_mail_set where EMAIL_TYPE='"+type+"'");
		
		return SqlSessionManager.getDataTable(sql);

	}
    
}
